package cn.cjxy.service;

import cn.cjxy.dao.HBaseDao;
import cn.cjxy.domain.TitleHot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TitleService {

    //依赖注入
    @Autowired
    private HBaseDao hbaseDao;

    /**
     * 基础时间范围查询
     * @param start
     * @param end
     * @return
     * @throws IOException
     */
    public List<TitleHot> findByTimeStamp(long start, long end) throws IOException {
        return hbaseDao.query(start, end);
    }

    /**
     * 统计某个时间段的前10数据
     * @param start   按出现次数降序排列
     * @param end
     * @return
     * @throws IOException
     */
    public List<TitleHot> findTitleByTimeStamp(long start, long end) throws IOException {
        //得到原始数据
        List<TitleHot> orginData = hbaseDao.query(start, end);
        //计数
        Map<String, Integer> map = new HashMap<>();
        for (TitleHot orginDatum : orginData) {//将title作为key  count作为value
            //当map中已经存在该key时，就在原有的value上加+1
            if (map.containsKey(orginDatum.getTitle())) {
                map.put(orginDatum.getTitle(), map.get(orginDatum.getTitle()) + 1);
            }else {
                map.put(orginDatum.getTitle(), 1); //如果不存在该key  value设置为1
            }
        }

        //不建议直接返回map
        List<TitleHot> list = new ArrayList<>();
        for (String s : map.keySet()) {//map.keySet()  返回所有的key集合
            TitleHot hot = new TitleHot();
            //设置标题
            hot.setTitle(s);
            //设置count
            hot.setCount(map.get(s));
            //添加到集合
            list.add(hot);
        }

        //排序 取前10
        list=list.stream().
                sorted((h1, h2) -> h1.getCount() - h2.getCount())
                //取前10  放回集合
                .limit(10).collect(Collectors.toList());
        return list;
    }
    //分终端统计

    /**
     *
     * @param start
     * @param end
     * @param agent
     * @return
     * @throws IOException
     */
    public List<TitleHot> findTitleByTimeStampAndAgent(long start, long end,String agent) throws IOException {
        //得到原始数据
        List<TitleHot> orginData = hbaseDao.query(start, end);
        //筛选终端  mobile   cinema  other
        List<TitleHot> list1=orginData.stream().filter(hot->hot.getAgentType().equals(agent)||"".equals(agent))
                .collect(Collectors.toList());
        //计数
        Map<String, Integer> map = new HashMap<>();
        for (TitleHot orginDatum : list1) {//将title作为key  count作为value
            //当map中已经存在该key时，就在原有的value上加+1
            if (map.containsKey(orginDatum.getTitle())) {
                map.put(orginDatum.getTitle(), map.get(orginDatum.getTitle()) + 1);
            }else {
                map.put(orginDatum.getTitle(), 1); //如果不存在该key  value设置为1
            }
        }

        //不建议直接返回map
        List<TitleHot> list = new ArrayList<>();
        for (String s : map.keySet()) {//map.keySet()  返回所有的key集合
            TitleHot hot = new TitleHot();
            //设置标题
            hot.setTitle(s);
            //设置count
            hot.setCount(map.get(s));
            //添加到集合
            list.add(hot);
        }

        //排序 取前10
        list=list.stream().
                sorted((h1, h2) -> h1.getCount() - h2.getCount())
                //取前10  放回集合
                .limit(10).collect(Collectors.toList());
        return list;
    }
}
